package basic;
//统计搜索过“%百度%”的所有用户ID
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Demo1 {

	/**统计练习，wordcount
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		if (args.length!=2) {
			System.err.print("user path err!!");
			System.exit(-1);
		}
		Job job = new Job(new Configuration(), "WordCount");
		job.setJarByClass(Demo1.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.setMapOutputKeyClass(mymap.class);
		job.setReducerClass(myreduce.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		job.waitForCompletion(true);
	}
	//map
	public static class mymap extends Mapper<LongWritable, Text, Text, IntWritable>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
			String[] line = value.toString().split(" ");
			for (String str : line) {
				context.write(new Text(str), new IntWritable(1));
			}
		};
	}
	//reduce
	public static class myreduce extends Reducer<Text,IntWritable,Text,IntWritable>{
		protected void reduce(Text k2, java.lang.Iterable<IntWritable> value, org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
			int sum=0;
			for (IntWritable count : value) {
				sum+=count.get();
			}
			context.write(k2, new IntWritable(sum));
		};
	}


}
